package ocr;


import java.util.HashMap;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.aip.ocr.AipOcr;
 
public class Recognition {
	public static final String APP_ID = "14850713";
    public static final String API_KEY = "1YxGh4ad70YjUSSMML0Ooppu";
    public static final String SECRET_KEY = "U1ObMWXz9RcrQGG8wUAcIk2PGKHM2ywi";

    public static String recognition(String filePath) {
    	 AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

         // 可选：设置网络连接参数
         client.setConnectionTimeoutInMillis(2000);
         client.setSocketTimeoutInMillis(60000);
         String resultStr = "";
         // 调用接口
         String path = filePath;
         org.json.JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
         JSONObject jsonObject = JSONObject.parseObject(res.toString());
         JSONArray resultList = jsonObject.getJSONArray("words_result");
         for(Object result:resultList){
             JSONObject temp = (JSONObject) result;
             resultStr += temp.getString("words");
         }
         return resultStr;
        
     }
}
